<template>
<el-drawer class="drawer" :title="drawer.title" :visible.sync="drawer.visible" direction="rtl" custom-class="drawer-right-side" :before-close="closeDrawer" :append-to-body="true">
    <el-form class="ruleForm" ref="ruleForm" :model="ruleForm" :rules="rules" label-position="top" label-width="100px">
        <el-form-item label="供应商简称" prop="title">
            <el-input v-model="ruleForm.title" placeholder="供应商简称"></el-input>
        </el-form-item>

        <el-form-item label="公司名称" prop="company_name">
            <el-input v-model="ruleForm.company_name" placeholder="公司名称"></el-input>
        </el-form-item>

        <el-form-item label="联系人" prop="contact_name">
            <el-input v-model="ruleForm.contact_name" placeholder="联系人"></el-input>
        </el-form-item>

        <el-form-item label="联系人电话" prop="contact_mobile">
            <el-input v-model="ruleForm.contact_mobile" placeholder="联系人电话"></el-input>
        </el-form-item>

        <el-form-item label="联系地址" prop="image">
            <el-input v-model="ruleForm.image" placeholder="联系地址"></el-input>
        </el-form-item>
        
        <el-form-item label="合作状态">
            <el-radio-group v-model="ruleForm.status">
                <el-radio :label="1">合作中</el-radio>
                <el-radio :label="0">暂停</el-radio>
            </el-radio-group>
        </el-form-item>

        <el-form-item label="排序" prop="sort">
            <el-input v-model="ruleForm.sort" type="number" placeholder="排序"></el-input>
        </el-form-item>

        <el-form-item>
            <el-button type="primary" @click="submit">确定并提交</el-button>
        </el-form-item>
    </el-form>
</el-drawer>
</template>

<script>
import {
    createSupplierApi,
    saveSupplierApi
} from '@/api/product/supplier';

const ruleForm = {
    id: "",
    title: "",
    company_name: "",
    contact_name: "",
    contact_mobile: "",
    address: "",
    sort: 0,
    status: 1
};

export default {
    data() {
        return {
            drawer: {
                title: "添加供应商",
                visible: false,
                type: "create"
            },
            ruleForm: Object.assign({}, ruleForm),
            rules: {
                title: [{
                    required: true,
                    message: "请添加",
                    trigger: "change",
                }, ],
            },
        };
    },
    methods: {
        showCreate() {
            this.ruleForm = Object.assign({}, ruleForm);
            this.drawer.visible = true;
            this.drawer.type = "create";
        },
        showEdit(row) {
            this.ruleForm = Object.assign({}, row);
            this.drawer.title = "编辑供应商信息";
            this.drawer.visible = true;
            this.drawer.type = "edit";
        },
        closeDrawer() {
            this.drawer.visible = false;
        },
        submit() {
            this.$refs["ruleForm"].validate((valid) => {
                if (this.drawer.type == "create") {
                    createSupplierApi(this.ruleForm).then(res => {
                        if (res.code == "200") {
                            this.$emit("getTableData");
                            this.closeDrawer();
                            this.$message.success(res.msg);
                        } else {
                            this.$message.error(res.msg);
                        }
                    })
                } else {
                    saveSupplierApi(this.ruleForm).then(res => {
                        if (res.code == "200") {
                            this.$emit("getTableData");
                            this.closeDrawer();
                            this.$message.success(res.msg);
                        } else {
                            this.$message.error(res.msg);
                        }
                    })
                }
            });
        },
    },
};
</script>

<style>
.contacts {
    margin: 18px 0;
}

.w100 {
    width: 100%;
}
</style>
